<template>
	<keep-alive :include="cachedViews">
		<router-view :key="key" :scrollTop="scrollTop" />
	</keep-alive>
</template>

<script>
// import CryptoJS from 'crypto-js'
import { resetRouter } from '@/router'
import { clearCookie, removeToken } from '@/utils/auth'
import { Storage } from '@/utils/compose'
export default {
  name: 'AppMain2',
  data() {
    return {
      wss: '',
      aesKey: '0123456789ABCDEF',
      timer: '',
      canWs: false,
      scrollTop: 1
    }
  },
  computed: {
    editVisible() {
      return this.$store.state.app.modifyVisible
    },
    cachedViews() {
      return this.$store.state.tagsView.cachedViews
    },
    key() {
      return this.$route.path
    }
  },
  mounted() {
    // this.ws();
    this.initUpcomingSelect()
    this._setInterval(this.initUpcomingSelect, 60000)()
    // 配合表格固定列表头的滚动
    this.$nextTick(() => {
      window.addEventListener(
        'scroll',
        () => {
          this.decrease(window.scrollY)()
        },
        true
      )
    })
  },
  destroyed() {
    window.clearTimeout(this.timer)
  },
  methods: {
    decrease(scrollTop) {
      let isDoing = false
      return () => {
        let timer = null
        if (isDoing) {
          return
        }
        isDoing = true
        timer = setTimeout(() => {
          isDoing = false
          this.scrollTop = scrollTop
          // console.log(this.scrollTop, 'scrollTop')
          clearTimeout(timer)
        }, 300)
      }
    },
    async initUpcomingSelect() {
      this.$api
        .getUpcomingSelect()
        .then((res) => {
          if (res?.code === 200) {
            const jsonStr = res.data
            // 会员模块
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.auditNewUser,
              type: 'auditNewUser' // 新增会员审核
            })
            // 新增商户审核
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.auditMerchantAdd,
              type: 'auditMerchantAdd'
            })
			// 总代分红审核
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.dividendAuditCount,
              type: 'dividendAuditCount'
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.auditUpdateInfoUser,
              type: 'auditUpdateInfoUser' // 会员账户修改审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.memberWithdrawToBeAuditCount,
              type: 'auditMemberWithdrawUser' // 会员提款审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.proxyWithdrawToBeAuditCount,
              type: 'auditProxyWithdrawAgent' // 代理提款审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.memberArtificialAccountAddCount,
              type: 'auditMemberArtificialAddUser' // 会员人工加额审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.proxyArtificialAccountAddCount,
              type: 'auditProxyArtificialAddAgent' // 代理人工加额审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.proxyCommissionAudit,
              type: 'auditProxyCommissionAgent' // 佣金审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.proxyRebateAudit,
              type: 'auditProxyRebateAgent' // 返点审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.memberActivityBonusToBeAuditCount,
              type: 'auditMemberActivityBonusUser' // 红利审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.memberBillWithdrawToBeAuditCount,
              type: 'auditMemberWithdrawSubUser' // 会员账单提现下分审核
            })
            // 代理模块
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.auditProxyAdd,
              type: 'auditProxyAdd' // 新增代理审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.auditUpdateInfoAgent,
              type: 'auditUpdateInfoAgent' // 代理账户修改审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.memberTransferProxyCount,
              type: 'auditMemberTransferUser' // 会员转代审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.proxyCommissionAuditCount,
              type: 'proxyCommissionAuditCount' // 佣金审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.proxyIncomeAudit,
              type: 'proxyIncomeAudit' // 会员转代审核
            })

            this.$store.dispatch('user/setAudit', {
              value: jsonStr.openProxyCount,
              type: 'openProxyReview' // 会员转代审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.upTopProxyCount,
              type: 'upTopProxyReview' // 代理换线审核
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.downloadFileCount,
              type: 'myDownloadBadge' // 我的下载文件数量
            })
            this.$store.dispatch('user/setAudit', {
              value: jsonStr.totalCreditAmount,
              type: 'totalCreditAmount' // 总代风控通知
            })
          }
        })
        .catch(() => {
          // this.loginOut()
        })
    },
    async loginOut() {
      // this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      this.$router.push(`/login`)
      removeToken()
      clearCookie()
      resetRouter()
      // 处理拖拽表格退出登录后保存拖拽的表头数据
      const newTableHeadData = Storage.get('commonTableKey')
      window.localStorage.clear()
      window.sessionStorage.clear()
      Storage.set('commonTableKey', newTableHeadData)
      await this.$store.dispatch('permission/clearRoutes')
      await this.$store.dispatch('tagsView/delAllViews')
    },
    _setInterval(fn, time) {
      const _this = this
      return function walk() {
        _this.timer = setTimeout(() => {
          fn()
          walk()
        }, time)
      }
    }
    // // aes加密
    // encrypt(data) {
    // 	var key = CryptoJS.enc.Utf8.parse(this.aesKey)
    // 	const encrypt = CryptoJS.AES.encrypt(data, key, {
    // 		iv: key,
    // 		mode: CryptoJS.mode.CBC,
    // 		padding: CryptoJS.pad.Pkcs7
    // 	})
    // 	return this.u8stringify(encrypt.ciphertext)
    // },
    // u8stringify(wordArray) {
    // 	var words = wordArray.words
    // 	var sigBytes = wordArray.sigBytes
    // 	var u8 = new Uint8Array(sigBytes)
    // 	for (var i = 0; i < sigBytes; i++) {
    // 		var byte = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff
    // 		u8[i] = byte
    // 	}
    // 	return u8
    // },
    // aes解密
    // decrypte(result) {
    // 	// 接收的是ArrayBuffer
    // 	// 将u8array转换成WordArray
    // 	var wordArray = CryptoJS.lib.WordArray.create(result)
    //
    // 	// 要求密文是base64格式
    // 	const data = wordArray.toString(CryptoJS.enc.Base64)
    //
    // 	// 解密key
    // 	var key = CryptoJS.enc.Utf8.parse(this.aesKey)
    //
    // 	var decrypted = CryptoJS.AES.decrypt(data, key, {
    // 		iv: key,
    // 		mode: CryptoJS.mode.CBC,
    // 		padding: CryptoJS.pad.Pkcs7
    // 	})
    //
    // 	var message = decrypted.toString(CryptoJS.enc.Utf8)
    // 	return message
    // 	// console.log(JSON.parse(JSON.parse(message).body))
    // },
    // sendHeart() {
    // 	const RequestPackage = {}
    // 	RequestPackage.clientTime = new Date().getTime()
    // 	this.send(1, this.$store.state.user.userInfo.id, RequestPackage)
    // },
    // send(commandId, playerId, obj) {
    // 	const RequestPackage = {}
    // 	RequestPackage.commandId = commandId
    // 	RequestPackage.playerId = playerId
    // 	RequestPackage.body = JSON.stringify(obj)
    // 	const m = JSON.stringify(RequestPackage)
    // 	this.wss.send(this.encrypt(m))
    // },
    // ws() {
    // 	const wsurl = process.env.VUE_APP_WSS
    // 	const url =
    // 		wsurl +
    // 		'playerId=' +
    // 		this.$store.state.user.userInfo.id +
    // 		'&jwtSys=1&jwtToken=' +
    // 		this.$store.state.user.token
    // 	this.wss = new WebSocket(url)
    // 	this.wss.binaryType = 'arraybuffer'
    // 	const _this = this
    // 	this.wss.onopen = function() {
    // 		// 成功后发起心跳
    // 		_this.timer = window.setInterval(_this.sendHeart, 5000)
    // 	}
    // 	this.wss.onmessage = function(event) {
    // 		const msg = JSON.parse(_this.decrypte(event.data))
    // 		const jsonStr = JSON.parse(JSON.parse(_this.decrypte(event.data)).body)
    // 		if (msg.commandId !== 1) {
    // 			console.log('有消息来了')
    // 			console.log(msg)
    // 			console.log(jsonStr)
    // 		}
    //
    // 		if (msg.commandId === 100) {
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditNewUser,
    // 				type: 'auditNewUser'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditUpdateInfoUser,
    // 				type: 'auditUpdateInfoUser'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditProxyAdd,
    // 				type: 'auditProxyAdd'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditUpdateInfoAgent,
    // 				type: 'auditUpdateInfoAgent'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditChangeAgent,
    // 				type: 'auditChangeAgent'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditPatchAgent,
    // 				type: 'auditPatchAgent'
    // 			})
    // 			// 资金模块
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditMemberWithdrawUser,
    // 				type: 'auditMemberWithdrawUser'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditProxyWithdrawAgent,
    // 				type: 'auditProxyWithdrawAgent'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditMemberArtificialAddUser,
    // 				type: 'auditMemberArtificialAddUser'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditProxyArtificialAddAgent,
    // 				type: 'auditProxyArtificialAddAgent'
    // 			})
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.auditProxyCommissionAgent,
    // 				type: 'auditProxyCommissionAgent'
    // 			})
    // 		} else if (msg.commandId === 101) {
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.count,
    // 				type: 'auditNewUser'
    // 			})
    // 		} else if (msg.commandId === 102) {
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.count,
    // 				type: 'auditUpdateInfoUser'
    // 			})
    // 		} else if (msg.commandId === 103) {
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.count,
    // 				type: 'auditProxyAdd'
    // 			})
    // 		} else if (msg.commandId === 104) {
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.count,
    // 				type: 'auditUpdateInfoAgent'
    // 			})
    // 		} else if (msg.commandId === 105) {
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.count,
    // 				type: 'auditChangeAgent'
    // 			})
    // 		} else if (msg.commandId === 106) {
    // 			_this.$store.dispatch('user/setAudit', {
    // 				value: jsonStr.count,
    // 				type: 'auditPatchAgent'
    // 			})
    // 		}
    // 	}
    // 	this.wss.onclose = function() {
    // 		window.clearInterval(_this.timer)
    // 		this.canWs = true
    // 		// TODO:暂时关闭重连机制
    // 		// setTimeout(() => {
    // 		// 	if (this.canWs) {
    // 		// 		_this.ws()
    // 		// 	}
    // 		// }, 10000)
    // 	}
    // 	this.wss.onerror = function() {
    // 		// console.log(evt)
    // 	}
    // }
  }
}
</script>

<style scoped>
.app-main {
	/*50 = navbar  */
	min-height: calc(100vh - 105px);
	width: 100%;
	position: relative;
	overflow: hidden;
}
.fixed-header + .app-main {
	padding-top: 105px;
}
</style>

<style lang="scss">
// fix css style bug in open el-dialog
// .el-popup-parent--hidden {
//   .fixed-header {
//     padding-right: 15px;
//   }
// }
</style>
